package Android.SinYateClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.util.Log;

public class Communication {
	private String ip;
	private int port;

	public Communication(String ip, int port) {
		System.out.println("new Communication: ip=" + ip + " - port=" + port);

		this.ip = ip;
		this.port = port;
	}

	public String register(String xml) {

		String uri = "";
		if (port != 80) {
			uri = "http://" + ip + ":" + port + "/register?id=0&data=" + xml;
		} else {
			uri = "http://" + ip + "/register?id=0&data=" + xml;
		}

		try {
			return sendMessage(uri);
		} catch (Exception e) {
			// FOUTAFHANDELING NOG DOEN
			// MessageBox.Show("registration failed, please check your internet connection and try again",
			// "Registration");
			Log.println(Log.ERROR, "ERROR", e.getMessage());
			return "";

		}
	}

	public String sendData(String id, String testid, String taskid, byte[] data) {

		String uri = "";
		if (port != 80) {
			uri = "http://" + ip + ":" + port + "/device" + id
					+ "?type=2&testid=" + testid + "&taskid=" + taskid;
		} else {
			uri = "http://" + ip + "/device" + id + "?type=2&testid=" + testid
					+ "&taskid=" + taskid;
		}
		try {
			return sendData(uri, data);

		} catch (Exception e) {
			return "";
		}
	}

	public String sendXML(String id, String testid, String taskid, String xml) {

		String uri = "";
		if (port != 80) {
			uri = "http://" + ip + ":" + port + "/device" + id
					+ "?type=3&testid=" + testid + "&data=" + xml + "&taskid="
					+ taskid;
		} else {
			uri = "http://" + ip + "/device" + id + "?type=3&testid=" + testid
					+ "&data=" + xml + "&taskid=" + taskid;
		}

		try {
			return sendMessage(uri);
		} catch (Exception e) {
			return "";
		}
	}

	public String sendPolling(String id, String xml) {
		String uri = "";
		if (port != 80) {
			uri = "http://" + ip + ":" + port + "/device" + id
					+ "?type=0&data=" + xml;
		} else {
			uri = "http://" + ip + "/device" + id + "?type=0&data=" + xml;
		}

		try {
			return sendMessage(uri);
		} catch (Exception e) {
			return "";
		}

	}

	private String sendMessage(String uri) throws Exception {

		URL url = new URL(uri);
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setDoOutput(true);
		connection.setRequestMethod("GET");
		connection.connect();

		BufferedReader stream = new BufferedReader(new InputStreamReader(
				connection.getInputStream()));

		String inputLine;
		String page = "";

		while ((inputLine = stream.readLine()) != null) {
			page += inputLine;
		}

		stream.close();
		connection.disconnect();

		return page;

	}

	public String sendImage(String id, String testid, String taskid, byte[] data) {

		String uri = "";
		if (port != 80) {
			uri = "http://" + ip + ":" + port + "/device" + id
					+ "?type=1&testid=" + testid + "&taskid=" + taskid;
		} else {
			uri = "http://" + ip + "/device" + id + "?type=1&testid=" + testid
					+ "&taskid=" + taskid;
		}
		try {
			return sendData(uri, data);

		} catch (Exception e) {
			return "";
		}
	}

	private String sendData(String uri, byte[] data)
			throws MalformedURLException, IOException {

		System.out.println("send: " + data.length);
		URL url = new URL(uri);
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setRequestProperty("Method", "POST");
		connection.setRequestProperty("Content-Type", "multipart/form-data");
		connection.setDoOutput(true);

		OutputStream out = connection.getOutputStream();

		out.write(data, 0, data.length);

		out.flush();
		out.close();

		BufferedReader stream = new BufferedReader(new InputStreamReader(
				connection.getInputStream()));

		if (stream == null) {
			return "";
		}

		String inputLine;
		String returndata = "";

		while ((inputLine = stream.readLine()) != null)
			returndata += inputLine;

		stream.close();
		connection.disconnect();
		return returndata.trim();
	}

}
